package class07_heapRelated;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class Student{
    public int id;
    public int age;
    public int score;

    public Student(int id, int age, int score){
        this.id = id;
        this.age = age;
        this.score = score;
    }
}


public class ComparatorPractise {


    public static Student[] randomStudentArr (int maxLen,int maxValue)
    {
        PriorityQueue<Integer> heap = new PriorityQueue<Integer>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return 0;
            }
        });
        int len = (int)(maxLen*Math.random());
        Student[] arr =new Student[len];

        for(int i = 0; i<len; i++)
        {
            arr[i] = new Student((int)(maxValue*Math.random()),
                                (int)(80*Math.random()),
                                (int)(100*maxValue*Math.random()));
        }
        return arr;
    }

    public static boolean isOrder(int[] arr)
    {
        int len = arr.length;

        for(int i = 0; i<len-1; i++)
        {
            if(arr[i+1]<arr[i])
            {
                return false;
            }
        }
        return true;
    }

    public static void swap(int[] arr, int index1, int index2)
    {

//        arr[index1] = arr[index1]^arr[index2];
//        arr[index2] = arr[index1]^arr[index2];
//        arr[index1] = arr[index1]^arr[index2];
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }

    public static void main(String[] args) {
        //Student 数组初始化时元素默认值是null
        Student[] stuArray = randomStudentArr(100,200);
        Arrays.sort(stuArray, new Comparator<Student>() {//比较器也可以用在有序结构里
            @Override
            public int compare(Student o1, Student o2) {
                //返回负数，则o1在前
                //返回正数，则o2在前
                //返回0，则两个在哪里都无所谓
                return o1.id-o2.id;
            }//o1在前面就是升序
        });
        for (Student student : stuArray) {
            System.out.print(student.id + " ");
        }
        System.out.println(" ");

    }
}
